package net.wlm.job;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import net.wlm.beans.VehicleData;
import net.wlm.utils.JdbcUtil;
import net.wlm.utils.KafkaUtil;
import org.apache.flink.api.common.functions.RichMapFunction;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;
import org.apache.flink.util.OutputTag;

public class CatLogEtlJob {
    public static void main(String[] args) throws Exception{
        // 创建环境
        StreamExecutionEnvironment  env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        // 数据读取
        DataStream<String> carLogStream = KafkaUtil.consumerKafka(env, "vehicle-log");
        // carLogStream.print();
        // 数据处理
        DataStream<String> cleanedLog = cleanLog(carLogStream);
        // 数据封装实体类
         DataStream<VehicleData>  vehicleStream = splitAndProcess(cleanedLog);

        // 写入CK
        // saveToCk(cleanedLog);

        // 数据转换
         DataStream<String> resultStream  = handle(vehicleStream);
        // 执行
        env.execute();
    }

    /**
     * 计算车辆数，电池健康度（使用batteryLevel）
     * @param vehicleStream
     */
    private static DataStream<String> handle(DataStream<VehicleData> vehicleStream) {
        KeyedStream<VehicleData, String> keyedStream = vehicleStream.keyBy(vehicleData -> vehicleData.getVin());
        return null;
    }

    private static void saveToCk(DataStream<String> cleanedLog) {
        SingleOutputStreamOperator<String> vehicleString = cleanedLog.map(new RichMapFunction<String, String>() {
            @Override
            public String map(String s) throws Exception {
                JSONObject parseObject = JSON.parseObject(s);
                JSONObject json = new JSONObject();
                json.put("vid", parseObject.getString("vin"));
                json.put("ts", parseObject.getLong("timestamp"));
                json.put("longitude", parseObject.getDouble("longitude"));
                json.put("latitude", parseObject.getDouble("latitude"));
                json.put("province", parseObject.getString("province"));
                json.put("speed", parseObject.getDouble("speed"));
                json.put("mileage", parseObject.getDouble("mileage"));
                json.put("batteryLevel", parseObject.getDouble("batteryLevel"));
                json.put("batteryTemp", parseObject.getDouble("batteryTemp"));
                json.put("motorTemp", parseObject.getDouble("motorTemp"));
                json.put("chargingStatus", parseObject.getString("chargingStatus"));
                json.put("energyConsumption", parseObject.getDouble("energyConsumption"));
                return json.toJSONString();
            }
        });

        String[] columns = new String[]{"vid", "ts", "longitude", "latitude","province" ,"speed", "mileage",
                "batteryLevel", "batteryTemp", "motorTemp", "chargingStatus", "energyConsumption"};

        // 插入sql
        String upsertSql = "insert into  vehicle_real.NEV_vehicle(vid, ts, longitude, latitude, province,speed, mileage,\n" +
                "batteryLevel, batteryTemp, motorTemp, chargingStatus, energyConsumption)\n" +
                "values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?)";

        // 插入数据
        JdbcUtil.sinkToClickhouseUpsertJson(vehicleString,  columns, upsertSql);
    }


    private static DataStream<String> cleanLog(DataStream<String> carLogStream) {
        // 定义脏数据侧边流
        final OutputTag<String> dirtyTag = new OutputTag<String>("dirty-data") {};
        SingleOutputStreamOperator<String> cleanedStream = carLogStream.process(new ProcessFunction<String, String>() {
            @Override
            public void processElement(String s, Context ctx, Collector<String> out) throws Exception {
                try {
                    // a.解析json数据
                    JSONObject json = JSON.parseObject(s);
                    // b.没有异常，解析正确，正常输出
                    out.collect(s);
                } catch (Exception e) {
                    ctx.output(dirtyTag, s);
                }
            }
        });
        // 侧边流输出
        KafkaUtil.producerKafka(cleanedStream.getSideOutput(dirtyTag), "vehicle-dirty-log");
        // 返回正常数据
        return cleanedStream;
    }
    private static DataStream<VehicleData> splitAndProcess(DataStream<String> carLogStream) {
        SingleOutputStreamOperator<VehicleData> vehicleStream = carLogStream.map(new RichMapFunction<String, VehicleData>() {
            @Override
            public VehicleData map(String s) throws Exception {
                JSONObject object = JSON.parseObject(s);
                VehicleData vehicleData = new VehicleData();
                vehicleData.setTs(object.getLong("ts"));
                vehicleData.setVin(object.getString("vin"));
                vehicleData.setLongitude(object.getDouble("longitude"));
                vehicleData.setLatitude(object.getDouble("latitude"));
                vehicleData.setSpeed(object.getDouble("speed"));
                vehicleData.setMileage(object.getDouble("mileage"));
                vehicleData.setBatteryLevel(object.getDouble("batteryLevel"));
                vehicleData.setBatteryTemp(object.getDouble("batteryTemp"));
                vehicleData.setMotorTemp(object.getDouble("motorTemp"));
                vehicleData.setChargingStatus(object.getString("chargingStatus"));
                vehicleData.setEnergyConsumption(object.getDouble("energyConsumption"));
                return vehicleData;
            }
        });
       return vehicleStream;
    }
}